package com.xc.hotelmgr.webapi.modules.op.controller;

import java.util.List;
import java.util.Map;

import com.xc.hotelmgr.webapi.common.annotation.SysLog;
import com.xc.hotelmgr.webapi.modules.dt.service.DtService;
import com.xc.hotelmgr.webapi.modules.op.entity.*;
import com.xc.hotelmgr.webapi.modules.op.service.*;
import com.xc.hotelmgr.webapi.modules.sys.controller.AbstractController;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.xc.hotelmgr.webapi.common.utils.PageUtils;
import com.xc.hotelmgr.webapi.common.utils.R;

/**
 * 
 *
 * @author kemmy
 * @email kemmy@gmail.com
 * @date 2020-09-29 14:30:31
 */
@EnableAsync
@RestController
@RequestMapping("op/htlgroup")
@RequiredArgsConstructor
public class HtlGroupController extends AbstractController {

    private final HtlGroupService htlGroupService;

    private final DtUpHtlService dtUpHtlService;

    private final DtService dtService;

    private final GroupService groupService;

    /**
     *
     * 查询分销商下拉列表
     *
     */
    @SysLog
    @RequestMapping("/listdt")
    @RequiresPermissions("op:htlgroup:list")
    public R listDt(@RequestParam Map<String, Object> params){
        params.put("dtId",getUserDtId());
        PageUtils page = dtService.queryPage(params);
        return R.ok().put("page", page);
    }
    /**
     * 列表
     */
    @SysLog
    @RequestMapping("/hotels")
    @RequiresPermissions("op:htlgroup:list")
    public R listHotel(@RequestParam Map<String, Object> params){
        PageUtils page = dtUpHtlService.sqlQueryPageByName(params);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @SysLog
    @RequestMapping("/list")
    @RequiresPermissions("op:htlgroup:list")
    public R list(@RequestParam Map<String, Object> params){
        String ownerDeptStr = getUserOwnerDept();
        String[] ownerDept = null;
        String dtId = (String) params.get("dtId");
        // 如果选择了机构，查询指定机构的数据
        if(!StringUtils.equals(dtId,getUserDtId().toString())){
            ownerDept = new String[1];
            ownerDept[0] = dtId;
        }else {
            ownerDept = ownerDeptStr.split("\\,");
        }
        params.put("ownerDept",ownerDept);
        PageUtils page = htlGroupService.sqlQueryPage(params);
        System.out.println(page.getList().size());
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @SysLog
    @RequestMapping("/info/{id}")
    @RequiresPermissions("op:htlgroup:info")
    public R info(@PathVariable("id") Integer id){
		HtlGroupEntity htlGroup = htlGroupService.getById(id);

        return R.ok().put("htlGroup", htlGroup);
    }

    /**
     * 保存
     */
    @SysLog
    @RequestMapping("/save")
    @RequiresPermissions("op:htlgroup:save")
    @Transactional
    public R save(@RequestBody GroupDto groupDto){
        R r = htlGroupService.saveHtlGroup(groupDto);
        List<HtlGroupEntity> htlGroupEntities = (List<HtlGroupEntity>) r.remove("htl");
        List<DtUpHtlEntity> dtUpHtlEntities = (List<DtUpHtlEntity>) r.remove("dthtl");
        if((int)r.get("code") == 0){
            dtUpHtlService.saveOrUpdateBatch(dtUpHtlEntities);
            groupService.saveHtlGroup(htlGroupEntities);
        }
        return r;
    }

    /**
     * 修改
     */
    @SysLog
    @RequestMapping("/update")
    @RequiresPermissions("op:htlgroup:update")
    public R update(@RequestBody HtlGroupEntity htlGroup){
		htlGroupService.updateById(htlGroup);
        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog
    @RequestMapping("/delete")
    @RequiresPermissions("op:htlgroup:delete")
    public R delete(@RequestBody List<HtlGroupEntity> htlGroupEntities) {
        //1、查询酒店分组下是否有自定义价格政策
        //2、如果有价格政策，先删除店铺上的自定义价格政策
        //3、删除本地库
        htlGroupService.delHtlGroup(htlGroupEntities);
        return R.ok();
    }
}
